/**
 * 按钮类型
 * @type {{CANCEL: string, CLOSE: string, OK: string}}
 */
export const BtnType = {
	OK: 'ok',
	CANCEL: 'cancel',
	CLOSE: 'close'
};
/**
 * 封装confirm，弹出框
 * @param args
 * @returns {*}
 */
const confirm = async (args) => {
	let params;
	if (typeof args === 'string') {
		params = { msg: args };
	} else {
		params = { ...args };
	}
	let infoDialog;
	return new Promise((resolve) => {
		// 显示挡住顶部
		window.showbarMask && window.showbarMask();
		infoDialog = window.Fiber.modal.info({
			class: 'vuex_sub_dep_modal',
			title: params.title,
			closable: true,
			content: (h) => (
				<div>
					<div class="vuex_sub_dep_modal_text">
						{params.msg}
					</div>
					<div class="vuex_sub_dep_modal_line"></div>
					<div class="vuex_sub_dep_modal_btns vuex_sub_dep_modal_btns_store">
						<div
							onClick={() => {
								//点击确定后执行的函数
								if (typeof params.ok_fn !== 'undefined') {
									// 传统回调
									params.ok_fn();
								}
								resolve(BtnType.OK);
							}}
						>
							{params.okText ? params.okText : $i18n('common.button.ok.label')}
						</div>
						<div
							onClick={() => {
								//点击取消后执行的函数
								if (typeof params.cancel_fn !== 'undefined') {
									// 传统回调
									params.cancel_fn();
								}
								resolve(BtnType.CANCEL);
							}}
						>{params.cancelText ? params.cancelText : $i18n('common.button.cancel.label')}</div>
					</div>
				</div>
			),
			onCancel() {
				resolve(BtnType.CLOSE);
			}
		}, window);
	})['finally'](() => {
		window.hidebarMask && window.hidebarMask();
		infoDialog.destroy();
	});
};


/**
 * 模拟v5的$.alert()
 * @param args
 * @returns {*}
 */
const alert = async (args) => {
	let params;
	if (typeof args === 'string') {
		params = { msg: args };
	} else {
		params = { ...args };
	}
	let infoDialog;
	return new Promise((resolve) => {
		// 显示挡住顶部
		window.showbarMask && window.showbarMask();
		infoDialog = window.Fiber.modal.info({
			class: 'vuex_sub_dep_modal',
			title: params.title,
			closable: true,
			content: (h) => (
				<div>
					<div class="vuex_sub_dep_modal_text">
						{params.msg}
					</div>
					<div class="vuex_sub_dep_modal_line"></div>
					<div class="vuex_sub_dep_modal_btns vuex_sub_dep_modal_btns_store">
						<div
							onClick={() => {
								//点击确定后执行的函数
								if (typeof params.ok_fn !== 'undefined') {
									// 传统回调
									params.ok_fn();
								}

								resolve(BtnType.OK);
							}}
						>
							{params.okText ? params.okText : $i18n('common.button.ok.label')}
						</div>
					</div>
				</div>
			),
			onCancel() {
				resolve(BtnType.CLOSE);
			}
		}, window);
	})['finally'](() => {
		window.hidebarMask && window.hidebarMask();
		infoDialog.destroy();
	});
};


export const Dialog = {
	confirm,
	alert
};
